package com.hyk.service.impl;

import com.hyk.entity.CodeGeneratorX;
import com.hyk.entity.request.CodeGeneratorRequest;
import com.hyk.entity.request.CodeGeneratorXRequest;
import com.hyk.entity.response.CodeGeneratorResponse;
import com.hyk.enums.ApiEnum;
import com.hyk.exception.GlobalException;
import com.hyk.mapper.CodeGeneratorXMapper;
import com.hyk.service.CodeGenerator;
import com.hyk.service.ICodeGeneratorXService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 代码生成器表 服务实现类
 * </p>
 *
 * @author hyk
 * @since 2021-03-10
 */
@Service
public class CodeGeneratorXServiceImpl extends ServiceImpl<CodeGeneratorXMapper, CodeGeneratorX> implements ICodeGeneratorXService {

    @Resource
    private CodeGeneratorXMapper codeGeneratorXMapper;

    @Resource
    private CodeGenerator codeGenerator;

    @Override
    public List<CodeGeneratorResponse> allList() {
        return codeGeneratorXMapper.allList();
    }

    @Override
    public List<CodeGeneratorResponse> listFor(CodeGeneratorRequest request) {
        return codeGeneratorXMapper.selectForList(request);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void execute(Long id) {
        CodeGeneratorXRequest codeGeneratorX = codeGeneratorXMapper.selectCodeGenerator(id);
        try{
            codeGenerator.generator(codeGeneratorX);
        }catch (Exception e){
            //
            throw new GlobalException(ApiEnum.CODE_GENERATOR_ERROR);
        }
        CodeGeneratorX code = new CodeGeneratorX();
        code.setStatus(1);
        code.setUpdateTime(new Date());
        codeGeneratorXMapper.updateById(code);
    }
}
